﻿import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import '../../../core/utils/common/common_util.dart';
import 'builder_forms_controller.dart';
import 'package:test_app/core/base/base/base_view.dart';
import 'package:test_app/core/design_system/extensions/extensions.dart';

class BuilderFormsView extends BaseView<BuilderFormsController> {
  const BuilderFormsView({super.key});

  @override
  String? get navTitle => "表单组件";

  @override
  Widget body() {
    return FormBuilder(
      key: controller.uiState.formKey,
      child: [
        _buildInput(label: "商品名称", field: 'productName').height(50),

        _buildRadio(
          label: "售卖渠道",
          field: 'saleChannel',
          options: controller.uiState.radioOptions,
        ).height(50),

        _buildRegion(
          label: "地址",
          field: 'address',
          options: controller.uiState.regionOptions,
          columnNum: 3,
        ).height(50),
        
      ].toColumn(
          mainAxisSize: MainAxisSize.min,
          separator: const Divider(height: 1),
      ).padAll(10).backgroundColor(Colors.amber).clipRadius(10).padAll(10),
    );
  }

  Widget _buildInput({
    required String label,
    required String field,
    String? hintText,
    validator,
  }) {
    return [
      Text(label),
      FormBuilderTextField(
        name: field,
        textAlign: TextAlign.right,
        decoration: InputDecoration(
          hintText: hintText ?? '请输入$label',
          border: InputBorder.none,
          isDense: true,
          contentPadding: EdgeInsets.zero,
        ),
        validator:
            validator ??
            FormBuilderValidators.compose([
              FormBuilderValidators.required(errorText: '请输入$label'),
            ]),
      ).expanded(),
    ].toRowBetween();
  }

  Widget _buildRadio({
    required String label,
    required String field,
    String? hintText,
    validator,
    required List<FormBuilderFieldOption<dynamic>> options,
  }) {
    return [
      Text(label),
      FormBuilderRadioGroup<dynamic>(
        name: field,
        wrapAlignment: WrapAlignment.end,
        decoration: InputDecoration(
          hintText: hintText ?? '请输入$label',
          border: InputBorder.none,
          isDense: true,
          contentPadding: EdgeInsets.zero,
        ),
        validator:
            validator ?? FormBuilderValidators.required(errorText: '请选择$label'),
        options: options,
      ).expanded(),
    ].toRowBetween();
  }

  Widget _buildRegion({
    required String label,
    required String field,
    String? hintText,
    validator,
    required Map<dynamic, dynamic> options,
    initialValue,
    required int columnNum,
  }) {
    return Builder(
      builder: (BuildContext context) {
        return FormBuilderField(
          name: field,
          initialValue: initialValue,
          validator:
              validator ??
              FormBuilderValidators.required(errorText: '请选择$label'),
          builder: (fieldState) {
            final values = fieldState.value ?? const <String>[];
            return [
              Text(label),
              [
                Text(
                  values is List && values.isNotEmpty
                      ? values.join(' ')
                      : hintText ?? '请选择$label',
                ),
                Icon(Icons.chevron_right),
              ].toRow(),
            ].toRowBetween().onTap(() {
              TDPicker.showMultiLinkedPicker(
                context,
                title: '$label选择',
                onConfirm: (selected) {
                  fieldState.didChange(selected);
                  CommonUtil.closePop();
                },
                data: controller.uiState.regionOptions,
                columnNum: columnNum,
                initialData: values is List ? values : initialValue ?? [],
              );
            });
          },
        );
      },
    );
  }

  Widget _cell(Widget child) {
    return child
        .height(60)
        .padHorizontal(10)
        .border(all: 1, color: Color(0xFF9191FF), radius: 10);
  }
}
